package com.example.test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import com.example.test.model.CommonFunc;
import com.example.test.model.Constants;
import com.example.test.model.DataBaseHelper;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuth;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.legacy.StatusesAPI;
import com.sina.weibo.sdk.openapi.legacy.WeiboAPI;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	Button btSubmit;
	EditText etAccount ;
	EditText etPasswd ;
	EditText etMail ;
	EditText etRePasswd ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
	    btSubmit=(Button)findViewById(R.id.btSubmit);
		etAccount=(EditText)findViewById(R.id.etAccount);
		etPasswd=(EditText)findViewById(R.id.etPasswd);
		etRePasswd=(EditText)findViewById(R.id.etRePasswd);
		etMail=(EditText)findViewById(R.id.etMail);
		setSubmitListener();
	}

	private void setSubmitListener()
	{
		btSubmit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(etAccount.getText().toString().equals(""))
				{
					Toast.makeText(RegisterActivity.this, "账号不能为空！", 1000).show();
				}
				else if(etPasswd.getText().toString().equals(""))
				{
					Toast.makeText(RegisterActivity.this, "密码不能为空！", 1000).show();
				}
				else if(etRePasswd.getText().toString().equals(""))
				{
					Toast.makeText(RegisterActivity.this, "重复密码不能为空！", 1000).show();
				}
				else if(etMail.getText().toString().equals(""))
				{
					Toast.makeText(RegisterActivity.this, "邮箱不能为空！", 1000).show();
				}
			    else if(!etRePasswd.getText().toString().equals(etPasswd.getText().toString()))
				{
					Toast.makeText(RegisterActivity.this, "密码不一致", 1000).show();
				}
			    else
			    {
				DataBaseHelper helper =new DataBaseHelper(RegisterActivity.this, "data", null, 2);
			    SQLiteDatabase database = helper.getWritableDatabase();
			    Cursor cursor = database.query("account", new String[] { "1" },"name=?",new String[]{ etAccount.getText().toString()},null,null,null);
			if(cursor.getCount()==0)
			{
				String name =etAccount.getText().toString();
				String passwd = etPasswd.getText().toString();
				String mail =etMail.getText().toString();
				ContentValues cv = new ContentValues();
				cv.put("name", name);
				cv.put("passwd", passwd);
				cv.put("mail", mail);
				long id = database.insert("account", null, cv);
				if(id==-1)
				{
					Toast.makeText(RegisterActivity.this, "数据插入失败！", 1000).show();
					return;
				}
				GlobalVar.userName=name;
				Toast.makeText(RegisterActivity.this, "注册成功！", 1000).show();
				SharedPreferences preferences = RegisterActivity.this.getSharedPreferences("default", MODE_PRIVATE);
				Editor editor=preferences.edit();
				editor.putString("account", name);
				editor.putString("passwd", passwd);
				editor.commit();
				Intent intent=new Intent();
				intent.setClass(RegisterActivity.this, BindAccountActivity.class);
				startActivity(intent);
				RegisterActivity.this.finish();
			}
			else 
			{
				Toast.makeText(RegisterActivity.this, "账号已存在，请重新输入", 1000).show();
			}
			    }
			}
		}
				);
		
	}
	
	
}
